package cn.com.xuxiaowei.service.impl;

import cn.com.xuxiaowei.bo.UserBo;
import cn.com.xuxiaowei.service.UserService;
import cn.com.xuxiaowei.vo.UserVo;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectWriter;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;

/**
 * 用户 WebService 接口
 *
 * @author 唐剑煌
 */
@Service
@WebService(targetNamespace = "http://service.caes.com/")
public class UserServiceImpl implements UserService {

    /**
     * 根据 用户ID 查询用户
     *
     * @param userBo 用户ID
     * @return 返回 用户
     */
    @Override
    @WebMethod
    @WebResult(name = "response")
    @RequestMapping(value = "getById", produces = MediaType.APPLICATION_XML_VALUE)
    public UserVo getById(@WebParam(name = "request") UserBo userBo) throws JsonProcessingException {
        UserVo userVo = new UserVo();

        /*String id = userBo.getId();

        userVo.setId(id);
        userVo.setUsername("用户-" + id);
        userVo.setPassword("用户-" + id + "-密码");*/


        return userVo;
    }

    /**
     * 节点前附加操作
     *
     * @param name
     */
    @Override
    @WebMethod
    public String flowCallback(@WebParam(name = "hdmc") String name) {
        System.out.println(name);
        return "success";
    }

    /**
     * 获取列表数据
     *
     * @return 返回 列表数据
     */
    @Override
    @RequestMapping(value = "getList", produces = MediaType.APPLICATION_XML_VALUE)
    public String getList(@WebParam(name = "username", mode = WebParam.Mode.IN, targetNamespace = "http://service.caes.com") String username) throws JsonProcessingException {
        // 请求参数
        UserVo.ListRequest request = new UserVo.ListRequest();
        request.setUsername("优惠券活动");
        request.setPassword("主键标识");

        // 在 WebService 方法名中设置参数
        UserVo.GetListId requestBodyGetById = new UserVo.GetListId();
        requestBodyGetById.setRequest(request);

        // 设置 WebService 方法名
        UserVo.GetListRequestBody getByIdRequestBody = new UserVo.GetListRequestBody();
        getByIdRequestBody.setGetById(requestBodyGetById);

        // 设置信封
        UserVo requestEnvelope = new UserVo();
        requestEnvelope.setBody(getByIdRequestBody);


        XmlMapper xmlMapper = new XmlMapper();
        ObjectWriter objectWriter = xmlMapper.writerWithDefaultPrettyPrinter();
        String envelopeXml = objectWriter.writeValueAsString(requestEnvelope);
        System.out.println("请求参数：\n" + envelopeXml);
        return envelopeXml;
    }

}
